package com.qf58.search.commons;


import com.qf58.search.page.BoolPager;
import com.qf58.search.page.LucenePager;
import com.qf58.search.pub.SearchFactor;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;

import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description: 工具类
 * User: zhubo
 * Date: 2017-08-29
 * Time: 16:28
 */
public class ESUtils {

    /**
     * 设置排序 高亮 和 分页
     * @param builder
     * @param pager
     * @return
     */
    public static SearchRequestBuilder getResponse(SearchRequestBuilder builder , LucenePager pager){
        //设置排序
        if(pager.getSortFields() != null && pager.getSortFields().size() > 0){
            Map<String,Integer> map = pager.getSortFields();
            Set<String> values = map.keySet();
            for (String field :values) {
                Integer value = map.get(field);
                SortBuilder sortBuilder = null;
                if(value.intValue() == SearchFactor.ASC){
                    sortBuilder = SortBuilders.fieldSort(field).order(SortOrder.ASC);
                }else{
                    sortBuilder = SortBuilders.fieldSort(field).order(SortOrder.DESC);
                }
                builder.addSort(sortBuilder);
            }
        }

        //设置高亮
        if(pager.getHighFields() != null && pager.getHighFields().length > 0){
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            for (String field : pager.getHighFields()) {
                highlightBuilder.field(field);
            }
            if(pager.getPreTag() == null || pager.getPostTag() == null){
                highlightBuilder.preTags("<b>").postTags("</b>");
            }else{
                highlightBuilder.preTags(pager.getPreTag()).postTags(pager.getPostTag());
            }
            builder.highlighter(highlightBuilder);
        }

        builder = builder.setFrom((int)pager.getStart()).setSize((int)pager.getPageSize());
        return builder;
    }


    /**
     * 根据pager信息获取 BoolQueryBuilder类 信息
     * @param pager
     * @return
     */
    public static BoolQueryBuilder getBoolQueryBuilder(BoolPager pager){
        //初始化boolQuery
        BoolQueryBuilder boolQueryBuilder = null;
        if((pager.getMustQuery() != null && pager.getMustQuery().size() > 0)||(pager.getFilterQuery() != null && pager.getFilterQuery().size() > 0) ||
                (pager.getMustNotQuery() != null && pager.getMustNotQuery().size() > 0) || (pager.getShouldQuery() != null && pager.getShouldQuery().size() > 0)){
            boolQueryBuilder = QueryBuilders.boolQuery();
        }
        //设置bool查询
        if(pager.getMustQuery() != null && pager.getMustQuery().size() > 0){
            Map<String,Object> map = pager.getMustQuery();
            Set<String> values = map.keySet();
            for (String field :values) {
                Object value = map.get(field);
                boolQueryBuilder.must(QueryBuilders.termQuery(field , value));
            }
            boolQueryBuilder.minimumShouldMatch(1);
        }

        //设置过滤查询
        if(pager.getFilterQuery() != null && pager.getFilterQuery().size() > 0){
            Map<String,Object> map = pager.getFilterQuery();
            Set<String> values = map.keySet();
            for (String field :values) {
                Object value = map.get(field);
                boolQueryBuilder.filter(QueryBuilders.termQuery(field , value));
            }
        }

        //设置MustNot查询
        if(pager.getMustNotQuery() != null && pager.getMustNotQuery().size() > 0){
            Map<String,Object> map = pager.getFilterQuery();
            Set<String> values = map.keySet();
            for (String field :values) {
                Object value = map.get(field);
                boolQueryBuilder.mustNot(QueryBuilders.termQuery(field , value));
            }
        }

        //设置should查询
        if(pager.getShouldQuery() != null && pager.getShouldQuery().size() > 0){
            Map<String,Object[]> map = pager.getShouldQuery();
            Set<String> values = map.keySet();
            for (String field :values) {
                Object[] value = map.get(field);
                for (Object obj : value) {
                    boolQueryBuilder.should(QueryBuilders.termQuery(field , obj));
                }
            }
        }

        return boolQueryBuilder;
    }

}
